//
//  HomeController.m
//  ZZmetro
//
//  Created by 一介布衣 on 2020/6/5.
//  Copyright © 2020 一介布衣. All rights reserved.
//

#import "HomeController.h"

#import "FuncAreaInfo.h"
#import "FuncInfo.h"
#import "HomeItemCell.h"
#import "HomeItemHeader.h"
#import "HomeData.h"
#import "HomeHeader.h"

@interface HomeController () <UICollectionViewDataSource, UICollectionViewDelegate>{
    NSMutableArray* funcAreaInfoArr;
    NSMutableArray* firstAreaInfos;
}
    

@property (strong, nonatomic) UICollectionView* collectionView;

@end

@implementation HomeController

- (instancetype)init {
    self = [super init];
    if (self) {
        funcAreaInfoArr = [HomeData getFuncAreaInfos];
        FuncAreaInfo* funcAreaInfo = [funcAreaInfoArr objectAtIndex:0];
        firstAreaInfos = funcAreaInfo.funcInfos;
    }
    
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupCollectionView];
}

//设置集合视图布局
- (void) setupCollectionView {
    
    // 1.创建流式布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(80, 80);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    CGSize screenSize  = [UIScreen mainScreen].bounds.size;
    if (screenSize.height > 568) {
        layout.itemSize = CGSizeMake(100, 116);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    layout.minimumInteritemSpacing = 5;
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 60);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    
    //设置可重用单元格标识与单元格类型
    [self.collectionView registerClass:[HomeHeader class]  forCellWithReuseIdentifier:@"homeHeader" ];
    [self.collectionView registerClass:[HomeItemCell class]  forCellWithReuseIdentifier:@"cellIdentifier" ];
    
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeItemHeader class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerIdentifier"];
    //[self.collectionView registerClass:[HomeItemHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Supple"];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.scrollEnabled = true;
    self.collectionView.alwaysBounceVertical = YES;
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.view addSubview:self.collectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return funcAreaInfoArr.count + 1;
}

//每个section中的列数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if(section == 0) {
        return 1;
    }
    
    FuncAreaInfo* funcAreaInfo = [funcAreaInfoArr objectAtIndex:section - 1];
    return funcAreaInfo.funcInfos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HomeHeader *headCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeHeader" forIndexPath:indexPath];
        return headCell;
    }
    
    HomeItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    FuncAreaInfo* funcAreaInfo = [funcAreaInfoArr objectAtIndex:indexPath.section - 1];
    NSMutableArray* funcInfos = funcAreaInfo.funcInfos;
    if (funcInfos.count <= indexPath.row) {
        return cell;
    }
    
    FuncInfo *info = funcInfos[indexPath.row];
    cell.label.text = info.name;
    cell.imageView.image = [UIImage imageNamed:info.img];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    FuncAreaInfo* funcAreaInfo = [funcAreaInfoArr objectAtIndex:indexPath.section];
    NSMutableArray* funcInfos = funcAreaInfo.funcInfos;
    FuncInfo *info = funcInfos[indexPath.row];
    NSLog(@"select event name : %@", info.name);
}

//header footter

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        HomeItemHeader *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerIdentifier" forIndexPath:indexPath];
        return reusableview;
    }
    return 0;
}


-(CGFloat)heightOfSectionHeaderForIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 0;
    } else {
        return 60;
    }
}


@end
